package com.example.mybatisplusdemo.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplusdemo.Methods.methods;
import com.example.mybatisplusdemo.Result.Result;
import com.example.mybatisplusdemo.Service.AdminService;
import com.example.mybatisplusdemo.Service.BusinessService;
import com.example.mybatisplusdemo.Service.GoodService;
import com.example.mybatisplusdemo.Service.OrderService;
import com.example.mybatisplusdemo.pojo.Business;
import com.example.mybatisplusdemo.pojo.Good;
import com.example.mybatisplusdemo.pojo.Order;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api")
@CrossOrigin
@Validated
public class BusinessController {
    @Resource
    BusinessService businessService;
    @Resource
    AdminService adminService;
    @Resource
    GoodService goodService;

    @Resource
    OrderService orderService;
    /**
     * 获取business列表
     * @param token
     * @return
     */
    @GetMapping("/getbus")
    public Result getbus(@RequestParam("token") String token){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        List<Business> list = businessService.list(new QueryWrapper<Business>().orderByDesc("id"));
        return new Result(200,"获取成功",list);
    }

    /**
     * 按名查找 bus
     * @param token
     * @param words
     * @return
     */
    @GetMapping("/getbusbyname")
    public Result getbusrbyname(@RequestParam("token") String token,
                                @RequestParam("words") String words){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        List<Business> list = businessService.list(new QueryWrapper<Business>().like("nike",words).orderByDesc("id"));
        return new Result(200,"查询成功",list);
    }

    /**
     * 添加商家
     * @param token
     * @param nike
     * @param loginname
     * @param pwd
     * @param tel
     * @param address
     * @return
     */
    @PostMapping("/addbus")
    public Result addbus(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                         @Valid @NotBlank(message = "昵称不能为空")@RequestParam("nike") String nike,
                         @Valid @NotBlank(message = "用户名不能为空")@RequestParam("loginname") String loginname,
                         @Valid @NotBlank(message = "密码不能为空")@RequestParam("pwd") String pwd,
                         @Valid @NotBlank(message = "电话不能为空")@RequestParam("tel") String tel,
                         @Valid @NotBlank(message = "地址不能为空")@RequestParam("address") String address
    ){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        if  (!new methods().checklenth(nike,3,10)) {return new Result(400,"昵称长度不正确");}
        if  (!new methods().checklenth(loginname,7,15)) {return new Result(400,"用户名长度不正确");}
        if  (!new methods().checklenth(pwd,6,15)) {return new Result(400,"密码长度不正确");}
        int count = businessService.count(new QueryWrapper<Business>().eq("loginname",loginname));
        if (count != 0){
            return new Result(400,"登录账号已经存在！");
        }
        Business business = new Business();
        business.setNike(nike);
        business.setLoginname(loginname);
        business.setPwd(new methods().md5(pwd));
        business.setTel(tel);
        business.setAddress(address);
        business.setToken(new methods().randomstr(14));
        boolean isSave = businessService.save(business);
        if (isSave){return new Result(200,"添加成功");}
        else{return new Result(400,"添加失败");}
    }

    /**
     * 删除bus
     * @param token
     * @param id
     * @return
     */
    @PostMapping("/deletebus")
    public Result deletebus(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                            @Valid @NotBlank(message = "id不能为空")@RequestParam("id") String id){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        int count = goodService.count(new QueryWrapper<Good>().eq("busid",id));
        if (count != 0){return new Result(400,"该商家存在商品未下架！");}
        boolean isDelete = businessService.remove(new QueryWrapper<Business>().eq("id",id));
        if (isDelete){return new Result(200,"删除成功");}
        else{return new Result(400,"删除失败");}
    }

    /**
     * 更新方法
     * @param token
     * @param id
     * @param nike
     * @param tel
     * @param address
     * @return
     */
    @PostMapping("/updatebus")
    public Result updatebus(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                            @Valid @NotBlank(message = "id不能为空")@RequestParam("id") String id,
                            @Valid @NotBlank(message = "昵称不能为空")@RequestParam("nike") String nike,
                            @Valid @NotBlank(message = "电话不能为空")@RequestParam("tel") String tel,
                            @Valid @NotBlank(message = "地址不能为空")@RequestParam("address") String address){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        if  (!new methods().checklenth(nike,3,10)) {return new Result(400,"昵称长度不正确");}
        Business business = businessService.getOne(new QueryWrapper<Business>().eq("id",id));
        business.setNike(nike);
        business.setTel(tel);
        business.setAddress(address);
        boolean isUpdate = businessService.update(business,new QueryWrapper<Business>().eq("id",id));
        if (isUpdate){return new Result(200,"更新成功");}
        else {return new Result(400,"更新失败");}
    }

    /**
     * 重置账号密码
     * @param token
     * @param id
     * @param loginname
     * @param pwd
     * @return
     */
    @PostMapping("/resetbus")
    public Result resetbus(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                           @Valid @NotBlank(message = "id不能为空")@RequestParam("id") String id,
                           @Valid @NotBlank(message = "用户名不能为空")@RequestParam("loginname") String loginname,
                           @Valid @NotBlank(message = "密码不能为空")@RequestParam("pwd") String pwd){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        Business business = businessService.getOne(new QueryWrapper<Business>().eq("id",id));
        if (!Objects.equals(business.getLoginname(), loginname)){
            int count = businessService.count(new QueryWrapper<Business>().eq("loginname",loginname));
            if (count != 0){return new Result(400,"登录账号已经存在");}
            business.setLoginname(loginname);
        }
        if (!Objects.equals(business.getPwd(), new methods().md5(pwd))){
            business.setPwd(new methods().md5(pwd));
        }
        boolean isUpdata = businessService.update(business,new QueryWrapper<Business>().eq("id",id));
        if (isUpdata){return new Result(200,"更新成功");}
        else{return new Result(400,"更新失败");}
    }


    /**
     * 获取商家信息
     * @param busid
     * @return
     */
    @GetMapping("/getBusDetail")
    public Result getBusDetail(@RequestParam("busid") String busid){
        Business business = businessService.getOne(new QueryWrapper<Business>().eq("id",busid));
        // 查询商品数量
        int goodCount = goodService.count(new QueryWrapper<Good>().eq("busid", busid));
        // 查询订单数量
        int orderCount = orderService.count(new QueryWrapper<Order>().eq("busid", busid));
        HashMap<String, Object> res = new HashMap<>();
        res.put("info", business);
        res.put("goodCount", goodCount);
        res.put("orderCount", orderCount);
        return new Result(200,"查询成功",res);
    }

    /**
     * 更新商家地址
     * @param busid
     * @param address
     * @return
     */
    @PostMapping("/editBusAddress")
    public Result updateBusAddress(@RequestParam("busid") String busid,@RequestParam("address") String address){
        Business business = businessService.getOne(new QueryWrapper<Business>().eq("id",busid));
        business.setAddress(address);
        boolean isUpdate = businessService.update(business,new QueryWrapper<Business>().eq("id",busid));
        if (isUpdate){return new Result(200,"更新成功");}
        else{return new Result(400,"更新失败");}
    }

    /**
     * 修改商家电话
     * @param busid
     * @param tel
     * @return
     */
    @PostMapping("/editBusTel")
    public Result updateBusTel(@RequestParam("busid") String busid,@RequestParam("tel") String tel){
        Business business = businessService.getOne(new QueryWrapper<Business>().eq("id",busid));
        business.setTel(tel);
        boolean isUpdate = businessService.update(business,new QueryWrapper<Business>().eq("id",busid));
        if (isUpdate){return new Result(200,"更新成功");}
        else{return new Result(400,"更新失败");}
    }
}
